/*********************************************** */
import { Message } from 'iview';
import theBaseUrl from './baseUrl'

console.log('here is theBaseUrl>>> ', theBaseUrl);

//相册
function pickPhoto() {
  navigator.camera.getPicture(function(imageURI) {
    var image = document.getElementById('logImage');
    image.src = imageURI;
  }, function(err) { console.log(err); }, {
    quality: 70,
    destinationType: navigator.camera.DestinationType.FILE_URI,
    sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
    saveToPhotoAlbum: true
  });
}
//拍照
function cameraTakePicture() {
  //  alert('cameraTakePicture');
  navigator.camera.getPicture(onSuccess, onFail, {
    quality: 50,
    destinationType: Camera.DestinationType.DATA_URL
  });

  function onSuccess(imageData) {
    var image = document.getElementById('logImage');
    image.src = 'data:image/jpeg;base64,' + imageData;
  }

  function onFail(message) {
    alert('拍照失败:' + message)
  }
}
//上传文件
function upload(fileURL, token, projectId, fn) {
  // alert('parseInt(userId)' + parseInt(userId));
  // alert('projectId' + projectId);
  // alert('fileURL' + fileURL);
  // alert('token' + token);

  //上传成功
  var success = function(data) {
      //  console.log("上传成功! Code = " + data.responseCode);
      // alert("上传成功! Code = " + data.responseCode);
      // alert(JSON.stringify(data.response))
      var res = JSON.parse(data.response);
      //显示返回的data内容
      // var r1 = JSON.stringify(data.response);
      // var dp2 = document.getElementById('dp2');
      // dp2.innerHTML = r1;
      var p1 = document.getElementById('p1');
      //   alert('>>>' + res);
      var p2 = document.getElementById('p2');
      var p3 = document.getElementById('p3');
      var p4 = document.getElementById('p4');

      //  p1.innerHTML = data.response;
      if (res.ret == -1) {
        // alert('*****>>>')
        if (res.errinfo.errcode == 5) {
          // alert('*****<<<<<');
          p1.innerHTML = "";
          p2.innerHTML = '请上传五官清晰的人脸照片';
          p3.innerHTML = '';
          p4.innerHTML = '';
        } else if (res.errinfo.errcode == 6) {
          p1.innerHTML = "";
          p2.innerHTML = '打卡失败';
          p3.innerHTML = '';
          p4.innerHTML = '';
        }
        fn();
      } else if (res.ret == 1) {
        var theName = res.info[0].name;
        if (theName == 'unknown') {
          p2.innerHTML = '打卡失败';
          p3.innerHTML = '';
          p4.innerHTML = '';
          fn()
        } else {
          p2.innerHTML = res.info[0].name;
          p3.innerHTML = res.info[0].pos;
          p4.innerHTML = res.info[0].tele;
          fn(theName);
        }
      }

    }
    //上传失败
  var fail = function(error) {
    // alert("上传失败! " + error.code);
    // alert("上传失败!");
    Message.error('上传失败');
    fn();
  }
  var options = new FileUploadOptions();
  options.fileKey = "photos";
  options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1) + '.jpg';
  options.mimeType = "image/jpeg";
  options.headers = new Map();
  var headers = { 'token': token }
  options.headers = headers;
  // options.headers['token'] = token;
  //上传参数
  var params = {};

  params.projectid = parseInt(projectId);
  // params.userid = parseInt(userId);
  // params.deviceType = deviceType;
  // alert(JSON.stringify(params))
  options.params = params;
  var ft = new FileTransfer();
  //上传地址
  // var SERVER = "http://120.27.21.149:9099/reco/recognition/checking_in"
  var SERVER = theBaseUrl + "/reco/recognition_1/checking_in"
    // alert(SERVER)
  ft.upload(fileURL, encodeURI(SERVER), success, fail, options);
};
export { cameraTakePicture, pickPhoto, upload };